package prv.pursuivant.config.impl;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import prv.pursuivant.config.Config;
import prv.pursuivant.config.ConfigSerializer;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.internal.Lists;

public class XStreamConfigSerializerTest {

	private Injector injector;

	@Before
	public void setUp() {
		injector = Guice.createInjector(new ConfigTestModule(),
				new XStreamConfigSerializerModule());
	}

	private String serialize(Config config) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(100);
		ConfigSerializer serializer = injector
				.getInstance(ConfigSerializer.class);
		serializer.writeConfig(config, baos);
		String configText = baos.toString();
		configText = configText.replaceAll(">\\s++<", "><");
		return configText;
	}

	@Test
	public void shouldWriteConfig() throws IOException {
		Config config = injector.getInstance(Config.class);
		String configText = serialize(config);
		final String expected = "<config><activeCategory id=\"168\"/>"
				+ "<importer region=\"eu\" realm=\"Shadowsong\" cacheDir=\"cache\" cacheTime=\"60\"/>"
				+ "<activeAchievements><achievement id=\"2137\"/><achievement id=\"2138\"/></activeAchievements>"
				+ "<toons><toon name=\"Kwinto\" rank=\"3\" active=\"true\"/></toons>"
				+ "</config>";
		assertEquals(expected, configText);
	}

	@Test
	public void shouldWriteConfigAchievements() throws IOException {
		List<String> achievs = Lists.newArrayList();
		achievs.add("2222");
		Config config = new ConfigImpl(null, null, achievs, null);
		String configText = serialize(config);
		assertEquals(
				"<config><activeAchievements><achievement id=\"2222\"/></activeAchievements></config>",
				configText);
	}

	@Test
	public void shouldReadConfigCategory() throws IOException {
		String configText = "<config><activeCategory id=\"168\"/></config>";
		final ByteArrayInputStream stream = new ByteArrayInputStream(configText
				.getBytes());
		ConfigSerializer serializer = injector
				.getInstance(ConfigSerializer.class);
		Config config = serializer.readConfig(stream);
		assertEquals("168", config.getActiveCategory().getId());
	}

	@Test
	public void shouldReadConfigCategoryAndAchievements() throws IOException {
		final FileInputStream stream = new FileInputStream(
				"testWD/config/testCategoryAndAchievements.xml");
		ConfigSerializer serializer = injector
				.getInstance(ConfigSerializer.class);
		Config config = serializer.readConfig(stream);
		assertEquals("168", config.getActiveCategory().getId());
		// Yes, I'm aware that comparing Collections by toString() methods may
		// not be the very best idea, but it's fast to write and clear to read
		// for now
		assertEquals("[2137, 2138]", config.getActiveAchievements().toString());
	}
}
